package other.edan.pass;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.data.model.Account;
import com.rkhd.platform.sdk.exception.ApiEntityServiceException;
import com.rkhd.platform.sdk.exception.XsyHttpException;
import com.rkhd.platform.sdk.http.RkhdHttpClient;
import com.rkhd.platform.sdk.http.RkhdHttpData;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import other.edan.pass.utils.HCXObjectService;

import java.io.IOException;
import java.util.List;

public class Tools {
    private static final Logger LOGGER = LoggerFactory.getLogger();


    /**
     * 工商信息精确查询
     * accurate
     */
    public static JSONObject accurateQueryOfBusinessInformation(String companynName) throws IOException, XsyHttpException {
        RkhdHttpData data = new RkhdHttpData();
        data.setCall_type("GET");
        String url = "/rest/data/v2.0/dataproduct/company/info?name=" + companynName;
        data.setCallString(url);
        JSONObject result = RkhdHttpClient.instance().execute(data, JSONObject::parseObject);
        LOGGER.info("结果:" + result.toJSONString());
        return result;
    }

    public static void main(String[] args) throws IOException, ApiEntityServiceException, XsyHttpException {
        //String sql="select id,accountName from account where unifiedSocialCreditIdentifier__c is null and entityType=3314472397658728";
        String sql = "select id,accountName from account where accountName='天津市多克隆商贸有限公司'";

        List<Account> accountList = HCXObjectService.queryAll(sql);
        for (Account account : accountList) {
            JSONObject result = accurateQueryOfBusinessInformation(account.getAccountName());
            if (null == result) {
                System.err.println("null");
                continue;
            }

            JSONArray array = result.getJSONObject("data").getJSONArray("records");
            if (array.size() <= 0) {
                continue;
            }

            Account newAccount = new Account();
            newAccount.setId(account.getId());
            newAccount.setUnifiedSocialCreditIdentifier__c(result.getJSONObject("data").getJSONArray("records").getJSONObject(0).getString("creditCode"));
            HCXObjectService.update(newAccount);
        }

    }
}
